<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.inp_house_code"
        placeholder="请输入仓库编码"
        clearable
      ></el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-select v-model="search.status" placeholder="请选择状态">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
       <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
      <el-button
        class="add"
        icon="el-icon-circle-plus"
        size="small"
        type="primary"
        @click="addAllot"
        >添加调拨单</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="serial_code"
        label="调拨单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusMsg"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="add_time"
        label="调拨时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inp_check_time"
        label="调入审核时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inp_house_code"
        label="仓库编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inp_house_name"
        label="仓库名称"
        align="center"
      ></el-table-column>

      <el-table-column label="操作" width="400" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="redact(scope)"
            v-if="scope.row.status != 2"
            >编辑</el-button
          >
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="handleEdit(scope)"
            >详情</el-button
          >
          <el-button
            v-if="scope.row.status != 2"
            size="mini"
            type="primary"
            icon="el-icon-edit-outline"
            @click="confirms(scope)"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope)"
            v-if="scope.row.status != 2"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- 详情Dailog -->
    <allotParticulars :dialog="dialog"></allotParticulars>
    <addAllot :addDialog="addDialog" @upData="getData"></addAllot>
  </div>
</template>
<script>
import { parseTime, getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import AllotParticulars from "@/components/allot/allotParticulars";
import AddAllot from "@/components/allot/addAllot";
import { getToken } from "@/utils/auth";
import {
  selectAllocationList,
  checkAllocationInfo,
  delAllocationInfo,
} from "@/api/allot/allotList";
export default {
  components: {
    pagination: Pagination,
    allotParticulars: AllotParticulars,
    addAllot: AddAllot,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        inp_house_code: "",
        status: "",
        start_time: "",
        end_time: "",
      },

      options: [
        // {
        //   value: 0,
        //   label: "待审核",
        // },
        {
          value: 1,
          label: "未审核",
        },
        {
          value: 2,
          label: "已审核",
        },
      ],
      //dialog信息
      dialog: {
        centerDialogVisible: false,
        id: "",
      },
      addDialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: "",
      },
      date: [],
    };
  },

  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        status: this.search.status,
        start_time: this.search.start_time,
        end_time: this.search.end_time,
        inp_house_code: this.search.inp_house_code,
      };
      selectAllocationList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.paginate.data.forEach((res) => {
              switch (res.status) {
                case 0:
                  res.statusMsg = "待审核";
                  break;
                case 1:
                  res.statusMsg = "未审核";
                  break;
                case 2:
                  res.statusMsg = "已审核";
                  break;
              }
            });
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //添加
    addAllot() {
      this.addDialog.centerDialogVisible = true;
      this.addDialog.dialogType = "";
      this.addDialog.id = "";
    },
    redact(scope) {
      this.addDialog.centerDialogVisible = true;
      this.addDialog.dialogType = "edit";
      this.addDialog.id = scope.row.id;
    },
    //删除
    handleDelete(scope) {
      let data = {
        id: scope.row.id,
      };
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delAllocationInfo(data).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg || res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //审核
    confirms(scope) {
      let data = {
        id: scope.row.id,
      };
      this.$confirm("是否审核信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          checkAllocationInfo(data).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消审核",
          });
        });
    },
    // 本地搜索
    handleSearch() {
      if (this.date) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //详情
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.id = scope.row.id;
    },

    batch(scope) {
      this.dialogs.centerDialogVisible = true;
      this.dialogs.id = scope.row.pickingid;
    },
     // 导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/api/allocation/exportAllocation?token=" +
            getToken() +
            "&status=" +
            this.search.status +
            "&start_time=" +
            this.search.start_time +
            "&end_time=" +
            this.search.end_time +
            "&inp_house_code=" +
            this.search.inp_house_code;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //清空
    empty() {
      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        searchCode: "",
        erpCode: [],
        status: "",
        start_time: this.normal_startime,
        end_time: this.normal_endtime,
      };
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}
.btnList {
  position: relative;
  margin-bottom: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>